﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project_2._2.Models;
using PagedList;
using PagedList.Mvc;
using Project_2._2.ViewModels;

namespace Project_2._2.Controllers
{
    public class XeController : Controller
    {
        private QuanLyThueXeEntities db = new QuanLyThueXeEntities();

        #region chi tiet xe
        // GET: /Xe/

        public ActionResult ChiTiet(int id)
        {
            CarList_ViewModel carslist = new CarList_ViewModel();
          
                        var cars = (from xe in db.Xes
                                    join kh in db.People on xe.PersonId equals kh.PersonId
                                    join gia in db.GiaXes on xe.XeId equals gia.XeId
                                    join dv in db.DichVuThueXes on gia.DichVuId equals dv.DichVuId
                                    join loai in db.LoaiXes on xe.LoaiXeId equals loai.LoaiXeId
                                    join th in db.ThuongHieux on xe.ThuongHieuId equals th.ThuongHieuId

                                    where xe.XeId == id && xe.isDelete == false
                                    select new
                                    {
                                        id = xe.XeId,
                                        name = xe.TenXe,
                                        THieuID = th.ThuongHieuId,
                                        tenloai = loai.TenLoaiXe,
                                        tenhieu = th.TenThuongHieu,
                                        Doixe = xe.DoiXe,
                                        DongXe = xe.DongXe,
                                        Color = xe.MauXe,
                                        LoaiID = loai.LoaiXeId,
                                        khID = kh.PersonId,
                                        tenKH = kh.HoTen,
                                        Gia = gia.Gia,
                                        Hinh = xe.HinhAnhDaiDien,
                                        TThai = xe.TrangThai,
                                        tenDV = dv.TenDichVu,
                                        iddv = dv.DichVuId,
                                        chitiet = xe.ChiTiet,
                                        phutroKM = xe.PhuTroQuaKm,
                                        phutroNgoaiGio = xe.PhuTroNgoaiGio,
                                        sudungcuoituan = xe.SuDungCuoiTuan,
                                        hinhanhdaidien = xe.HinhAnhDaiDien,
                                        video = xe.Video,
                                        trangthai = xe.TrangThai,
                                        duyet = xe.Duyet,

                                    }).ToList();

                        foreach (var item in cars)
                        {
                            carslist.id = item.id;
                            carslist.name = item.name;
                            carslist.ThuongHieuID = item.THieuID;
                            carslist.Loai = item.tenloai;
                            carslist.THieu = item.tenhieu;
                            carslist.DoiXe = item.Doixe;
                            carslist.DongXe = item.DongXe;
                            carslist.Color = item.Color;
                            carslist.LoaiID = item.LoaiID;
                            carslist.TThai = item.TThai;
                            carslist.Gia = item.Gia;
                            carslist.khachHangID = item.khID;
                            carslist.TenKH = item.tenKH;
                            carslist.hinh = item.Hinh;
                            carslist.dichvuID = item.iddv;
                            carslist.tendichvu = item.tenDV;
                            carslist.ChiTiet = item.chitiet;
                            carslist.PhuTroKM = item.phutroKM;
                            carslist.PhuTroNgoaiGio = item.phutroNgoaiGio;
                            carslist.SuDungCuoiTuan = item.sudungcuoituan;
                            carslist.Video = item.video;
                            carslist.TThai = item.trangthai;
                            carslist.duyet = item.duyet;
                            carslist.hinh = item.hinhanhdaidien;
                        }

                        return View(carslist);
         
        }

        #endregion

        #region danh sach xe theo dich vu
        public ActionResult DichVu(int? page, int id)
        {
            ViewBag.id = id;

            List<CarList_ViewModel> listcar = new List<CarList_ViewModel>();

            var cars = (from xe in db.Xes
                        join gia in db.GiaXes on xe.XeId equals gia.XeId
                        join dv in db.DichVuThueXes on gia.DichVuId equals dv.DichVuId
                        join loai in db.LoaiXes on xe.LoaiXeId equals loai.LoaiXeId
                        join th in db.ThuongHieux on xe.ThuongHieuId equals th.ThuongHieuId
                        where gia.DichVuId == id && gia.isDelete == false && xe.Duyet == true && xe.isDelete == false
                        select new
                        {
                            id = xe.XeId,
                            name = xe.TenXe,
                            THieu = th.TenThuongHieu,
                            Doixe = xe.DoiXe,
                            DongXe = xe.DongXe,
                            Color = xe.MauXe,
                            Loai = loai.TenLoaiXe,
                            Gia = gia.Gia,
                            Hinh = xe.HinhAnhDaiDien,
                            TThai = xe.TrangThai,
                            tenDV = dv.TenDichVu,
                            iddv = dv.DichVuId
                        }).OrderByDescending(m => m.id).ToList();
            CarList_ViewModel carslist;
            if (cars.Count > 0)
            {
                foreach (var item in cars)
                {
                    carslist = new CarList_ViewModel();
                    carslist.id = item.id;
                    carslist.name = item.name;
                    carslist.THieu = item.THieu;
                    carslist.DoiXe = item.Doixe;
                    carslist.DongXe = item.DongXe;
                    carslist.Color = item.Color;
                    carslist.Loai = item.Loai;
                    carslist.TThai = item.TThai;
                    carslist.Gia = item.Gia;
                    carslist.hinh = item.Hinh;
                    carslist.dichvuID = item.iddv;
                    listcar.Add(carslist);
                }
                var carReturn = listcar.ToPagedList(page ?? 1, 10);
                ViewBag.TieuDe = cars[0].tenDV;
                return View(carReturn);
            }

            return View();
        }

        #endregion

        #region danh sach xe theo loai xe
        public ActionResult LoaiXe(int? page, int id, int dvu)
        {
            ViewBag.id = id;
            ViewBag.dvu = dvu;
            List<CarList_ViewModel> listcar = new List<CarList_ViewModel>();

            var cars = (from xe in db.Xes
                        join gia in db.GiaXes on xe.XeId equals gia.XeId
                        join dv in db.DichVuThueXes on gia.DichVuId equals dv.DichVuId
                        join loai in db.LoaiXes on xe.LoaiXeId equals loai.LoaiXeId
                        join th in db.ThuongHieux on xe.ThuongHieuId equals th.ThuongHieuId
                        where gia.DichVuId == dvu && xe.LoaiXeId == id && gia.isDelete == false && xe.Duyet == true && xe.isDelete == false
                        select new
                        {
                            id = xe.XeId,
                            name = xe.TenXe,
                            THieu = th.TenThuongHieu,
                            Doixe = xe.DoiXe,
                            DongXe = xe.DongXe,
                            Color = xe.MauXe,
                            Loai = loai.TenLoaiXe,
                            Gia = gia.Gia,
                            Hinh = xe.HinhAnhDaiDien,
                            TThai = xe.TrangThai,
                            tenDV = dv.TenDichVu,
                             iddv = dv.DichVuId
                        }).OrderByDescending(m => m.id).ToList();
            CarList_ViewModel carslist;
            if (cars.Count > 0)
            {
                foreach (var item in cars)
                {
                    carslist = new CarList_ViewModel();
                    carslist.id = item.id;
                    carslist.name = item.name;
                    carslist.THieu = item.THieu;
                    carslist.DoiXe = item.Doixe;
                    carslist.DongXe = item.DongXe;
                    carslist.Color = item.Color;
                    carslist.Loai = item.Loai;
                    carslist.TThai = item.TThai;
                    carslist.Gia = item.Gia;
                    carslist.hinh = item.Hinh;
                    carslist.dichvuID = item.iddv;
                    listcar.Add(carslist);
                }
                var carReturn = listcar.ToPagedList(page ?? 1, 1);
                ViewBag.TieuDe = cars[0].Loai;
                return View(carReturn);
            }

            return View();
            //return View(car);
        }

        #endregion

        #region danh sach xe theo thuong hieu
        public ActionResult ThuongHieu(int? page, int id)
        {
            ViewBag.id = id;

            List<CarList_ViewModel> listcar = new List<CarList_ViewModel>();

            var cars = (from xe in db.Xes
                        join loai in db.LoaiXes on xe.LoaiXeId equals loai.LoaiXeId
                        join gia in db.GiaXes on xe.XeId equals gia.XeId
                        join th in db.ThuongHieux on xe.ThuongHieuId equals th.ThuongHieuId
                        join dv in db.DichVuThueXes on gia.DichVuId equals dv.DichVuId
                        where th.ThuongHieuId == id && xe.Duyet == true && xe.isDelete == false
                        select new
                        {
                            id = xe.XeId,
                            name = xe.TenXe,
                            THieu = th.TenThuongHieu,
                            Doixe = xe.DoiXe,
                            DongXe = xe.DongXe,
                            Color = xe.MauXe,
                            Loai = loai.TenLoaiXe,
                            Hinh = xe.HinhAnhDaiDien,
                            TThai = xe.TrangThai,
                            dvid = dv.DichVuId,
                        }).OrderByDescending(m => m.id).ToList();
            CarList_ViewModel carslist;
            if (cars.Count > 0)
            {
                foreach (var item in cars)
                {
                    carslist = new CarList_ViewModel();
                    carslist.id = item.id;
                    carslist.name = item.name;
                    carslist.THieu = item.THieu;
                    carslist.DoiXe = item.Doixe;
                    carslist.DongXe = item.DongXe;
                    carslist.Color = item.Color;
                    carslist.Loai = item.Loai;
                    carslist.TThai = item.TThai;
                    carslist.Gia = 0;
                    carslist.hinh = item.Hinh;
                    carslist.dichvuID = item.dvid;
                    listcar.Add(carslist);
                }
                var carReturn = listcar.ToPagedList(page ?? 1, 2);
                ViewBag.TieuDe = cars[0].THieu;
                return View(carReturn);
            }

            return View();
        }

        #endregion
        public ActionResult TinTucChiTiet(int id)
        {
            var tt = (from p in db.TinTucs where p.TinTucId == id select new { p.TieuDe, p.NoiDung}).FirstOrDefault();
            if (tt != null)
            {
                TinTuc_ViewModels tintuc = new TinTuc_ViewModels();
                tintuc.TieuDe = tt.TieuDe;
                tintuc.NoiDung = tt.NoiDung;
                return View(tintuc);
            }
            return View();
        }

       
    }
}
